package sunstar.com.xq.activity

import android.os.Bundle
import android.os.CountDownTimer
import android.support.design.widget.Snackbar
import kotlinx.android.synthetic.main.activity_base.*
import kotlinx.android.synthetic.main.activity_regist.*
import org.jetbrains.anko.AnkoLogger
import org.jetbrains.anko.info
import org.jetbrains.anko.toast
import sunstar.com.xq.R
import sunstar.com.xq.presenter.RegistPresenter
import sunstar.com.xq.util.DialogUtil
import sunstar.com.xq.view.RegistView
import java.util.*
import kotlin.concurrent.timerTask

class RegistActivity : BaseActivity(), AnkoLogger, RegistView {


    override fun setBaseView(): Int = R.layout.activity_regist

    override fun setTitleText(): String = ""

    override fun showTitle(): Boolean = false
    var click = true
    var registPresenter = RegistPresenter(this)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        regist_getcode.setOnClickListener {
            registPresenter.getCode(regist_username.text.toString())
        }
        regist_btn.setOnClickListener {
            if (registPresenter.checkNumber(regist_username.text.toString(), regist_code.text.toString(), regist_password.text.toString())
                    ) {
                dialoUtil!!.show()
                registPresenter.regist(regist_username.text.toString(), regist_code.text.toString(), regist_password.text.toString())
            }
        }
    }

    override fun getCode() {
        if (click) {
            object : CountDownTimer(60000, 1000) {
                override fun onFinish() {
                    click = true
                    regist_getcode.progress = 100
                }

                override fun onTick(p0: Long) {
                    click = false
                    var progressD = 0
                    progressD = 100 - ((((p0 / 1000).toInt()) * 100) / 60)
                    regist_getcode.progress = progressD
                }
            }.start()
        }
    }

    override fun codeError() {
        Snackbar.make(regist_getcode, "请输入正确手机号", 2000).show()
    }

    override fun numberError(message: String) {
        Snackbar.make(regist_getcode, message, 2000).show()
    }

    override fun passwordError(message: String) {
        Snackbar.make(regist_getcode, message, 2000).show()
    }

    override fun registComplet(message: String) {
        dialoUtil!!.dismiss()
        toast(message)
        finish()
    }

    override fun registError(message: String) {
        Snackbar.make(regist_getcode, message, 2000).show()
    }
}
